<?php
include("account.php");
$user_id = GetLoginId($_COOKIE['t']);

$orig_url = $_GET['d'];
if ($user_id && $user_id > 0) {
  $res_word = "用户已经登录";
  header('Location: ' . $URL_MY_PAGE);
} else {
  $user_email = $_POST['email'];
  $user_pwd = $_POST['uk'];
  if (!$user_email || !$user_pwd) {
    $res_word = "请输入用户名密码";
  } else {
    $conn = mysql_connect("127.0.0.1","root","");
    mysql_select_db("user_account");
    
    $sql=sprintf("SELECT id, password FROM password WHERE email='%s'", mysql_real_escape_string($user_email));
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);
    if (!$row) {
      $res_word = "用户不存在";
    } else {
      // echo $row['password'] . '---' . $pwd_hash;
      $pwd_hash = GetPasswordHash($user_pwd);
      if ($row['password'] == $pwd_hash) {
        $res_word = "登录成功";
        $user_id = $row['id'];
        $user_ticket = md5($user_email . "-salt+" . $user_pwd . "#moresalt*" . time());
        SetLoginId($user_id, $user_ticket);
        setcookie('id', $user_id, time() + 30 * 24 * 60 * 60, "/", '.jztzp.com');
        setcookie('t', $user_ticket, 0, "/", '.jztzp.com');
        // echo "登录成功";
        if ($orig_url) {
          header('Location: ' . $orig_url);
        } else {
          header('Location: ' . $URL_MY_PAGE);
        }
      } else {
        $res_word = "密码错误";
      }
    }
    mysql_close($conn);
  }
}
?>
<?php
$page_title = "用户登录";
include("../header.php");
?>
<style type="text/css">
label.account_input_name {
display:inline-block;line-height:30px;width:100px;
}
input.account_input {
vertical-align: middle;margin:-6px 0 0 0px;padding: 6px 4px 6px;width:216px;font-size:90%;border:1px solid #cbc;
}
input.account_input_submit{
  padding:4px 2px;
  cursor:pointer;
  cursor:hand;
  font-weight:bold;
  font-size:18px;
  height:30px;
  width:86px;
  text-align:center;
  border: 1px solid #CCCCCC;
  color: #183D5C;
  background-color:#F6F6F6;
}
</style>
<div class="container">
<div style="line-height:30px;font-size:18px;margin:40px 70px 340px">
<form method="POST" atcion="/account/login.php">
<p>
<label class="account_input_name">邮箱:</label> <input class="account_input" id="email_input" name="email" type="text"/>
</p>
<p>
<label class="account_input_name">密码:</label> <input class="account_input" id="pwd_input" name="uk" type="password"/>
</p>
<input type="submit" class="account_input_submit" value="登录"/> &nbsp; <a style="font-size:70%;" href="<?php echo $URL_REGISTER_PAGE ?>">注册</a>
</form>
</div>
</div>

<script type="text/javascript">
function ValidateEmail(email) { 
  var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  return re.test(email);
}

$('#email_input').keyup(
  function(event) {
    var email = $('#email_input', $(this).parent()).val();
    /*
    if (ValidateEmail(email)) {
      $('#email_input', $(this).parent()).css("border", "1px solid green");
    } else {
      $('#email_input', $(this).parent()).css("border", "1px solid red");
    }
     */
  }
);

$('#email_input').keyup(
  function(event) {
    var pwd = $('#pwd_input', $(this).parent()).val();
  }
);

</script>
<?php
include("../footer.php");
?>

